package com.shop.trade.api.controller;

import com.shop.common.model.InputBean;
import com.shop.common.model.param.trade.OrderCommentInput;
import com.shop.common.tmpl.BaseController;
import com.shop.common.util.SessionUtils;
import com.shop.common.util.excption.UserNotFoundException;
import com.shop.trade.api.api.IOrderCommentApi;
import com.shop.trade.api.service.IOrderCommentSvc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description:
 *
 * @author zhoulk
 * @date: 2019年04月16日  3:41
 */
@RestController
public class OrderCommentController extends BaseController implements IOrderCommentApi {

    @Autowired
    SessionUtils sessionUtils;
    @Autowired
    IOrderCommentSvc iOrderCommentSvc;

    @Override
    public String addComment(@RequestBody OrderCommentInput orderCommentInput, HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        return returnSvc(iOrderCommentSvc.addComment(orderCommentInput, userId));
    }

    @Override
    public String modComment(@RequestBody OrderCommentInput input, HttpServletRequest request) {
        return returnSvc(iOrderCommentSvc.modComment(input));
    }

    @Override
    public String delComment(Integer id, HttpServletRequest request) {
        return returnSvc(iOrderCommentSvc.delComment(id));
    }

    @Override
    public String qryComment(OrderCommentInput orderCommentInput, HttpServletRequest request) {
        return returnSvc(iOrderCommentSvc.qryOrderComment(orderCommentInput));
    }

    @Override
    public String qryComments(OrderCommentInput orderCommentInput, HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        if (orderCommentInput.getBuyerId() != null) {
            orderCommentInput.setBuyerId(userId);
        }
        return returnSvc(iOrderCommentSvc.qryOrderComments(orderCommentInput));
    }

    @Override
    public String qryComments4P(OrderCommentInput orderCommentInput, Integer index, Integer pageSize, HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        orderCommentInput.setBuyerId(orderCommentInput.getBuyerId()!=null&&orderCommentInput.getBuyerId()==-1?userId:orderCommentInput.getBuyerId());
        orderCommentInput.setMchtId(orderCommentInput.getMchtId()!=null&&orderCommentInput.getMchtId()==-1?userId:orderCommentInput.getMchtId());
        InputBean<OrderCommentInput> inputBean = new InputBean<>(orderCommentInput);
        inputBean.setIndex(index);
        inputBean.setPageSize(pageSize);
        return returnSvc(iOrderCommentSvc.qryOrderComments4P(inputBean));
    }


    @Override
    public String commentCount(OrderCommentInput orderCommentInput,HttpServletRequest request) throws UserNotFoundException {
        Integer userId = sessionUtils.USER_ID(request);
        orderCommentInput.setBuyerId(orderCommentInput.getBuyerId()!=null&&orderCommentInput.getBuyerId()==-1?userId:orderCommentInput.getBuyerId());
        orderCommentInput.setMchtId(orderCommentInput.getMchtId()!=null&&orderCommentInput.getMchtId()==-1?userId:orderCommentInput.getMchtId());
        return returnSvc(iOrderCommentSvc.qryCommentCount(orderCommentInput));
    }
}
